package pl.olap.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;

/**
 * 
 * @author Wojtek
 * 
 */
public class Connector {

	private URLConnection myURLConnection = null;
	private String url = "";

	public Connector() {

	}

	public Connector(String p_url) {
		this.url = p_url;
		URL myURL;
		try {
			myURL = new URL(p_url);
			myURLConnection = myURL.openConnection();
			myURLConnection.setUseCaches(true);
			myURLConnection.connect();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}

	public void connect(String p_url) {
		this.url = p_url;
		try {
			URL myURL = new URL(p_url);
			myURLConnection = myURL.openConnection();
			myURLConnection.setUseCaches(true);
			myURLConnection.connect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public String getContent() throws IOException {
		if (myURLConnection == null) {
			return null;
		}
		BufferedReader in = new BufferedReader(new InputStreamReader(
				myURLConnection.getInputStream()));
		String inputLine, result = new String("");
		while ((inputLine = in.readLine()) != null)
			//MimeUtility.encodeText(body,"UTF-8","BASE64") //URLDecoder.decode(inputLine, "UTF-8")
			result = result.concat(inputLine);
		in.close();
		return result;

	}

	public String getUrl() {
		return this.url;
	}

}
